package com.zhongen.oa.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.oa.entity.SysCarEntity;
import com.zhongen.oa.service.SysCarService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆表
 *
 * @author Code Generator
 * @date 2024-02-07 09:03:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysCar")
@Tag(description = "sysCar", name = "车辆表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysCarController {

	private final SysCarService sysCarService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param sysCar 车辆表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_sysCar_view')")
	public R getSysCarPage(@ParameterObject Page page, @ParameterObject SysCarEntity sysCar) {
		LambdaQueryWrapper<SysCarEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(sysCarService.page(page, wrapper));
	}

	/**
	 * 查询全部
	 * @return
	 */
	@Operation(summary = "查询全部", description = "分页查询")
	@GetMapping("/getSysCarList")
	@PreAuthorize("@pms.hasPermission('oa_sysCar_view')")
	public R getSysCarList() {
		LambdaQueryWrapper<SysCarEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(sysCarService.list(wrapper));
	}

	/**
	 * 通过id查询车辆表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_sysCar_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(sysCarService.getById(id));
	}

	/**
	 * 新增车辆表
	 * @param sysCar 车辆表
	 * @return R
	 */
	@Operation(summary = "新增车辆表", description = "新增车辆表")
	@SysLog("新增车辆表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_sysCar_add')")
	public R save(@RequestBody SysCarEntity sysCar) {
		return R.ok(sysCarService.save(sysCar));
	}

	/**
	 * 修改车辆表
	 * @param sysCar 车辆表
	 * @return R
	 */
	@Operation(summary = "修改车辆表", description = "修改车辆表")
	@SysLog("修改车辆表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_sysCar_edit')")
	public R updateById(@RequestBody SysCarEntity sysCar) {
		return R.ok(sysCarService.updateById(sysCar));
	}

	/**
	 * 通过id删除车辆表
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除车辆表", description = "通过id删除车辆表")
	@SysLog("通过id删除车辆表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_sysCar_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(sysCarService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param sysCar 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_sysCar_export')")
	public List<SysCarEntity> export(SysCarEntity sysCar, Long[] ids) {
		return sysCarService.list(Wrappers.lambdaQuery(sysCar).in(ArrayUtil.isNotEmpty(ids), SysCarEntity::getId, ids));
	}

}